<?php
namespace App\Http\Controllers\AdminApi;

use App\Model\AdminTable;
use App\Model\ClassTable;
use App\Model\CouponListTable;
use App\Model\CouponTable;
use App\Model\UserTable;
use App\Repository\BaseController\ApiController;
use App\Repository\Common\ConstTable;
use App\Repository\Sys\JsonRespose;
use App\Repository\Sys\Str;

class CouponController extends ApiController
{


    /**
     * @api {get} /adminapi/coupon/list 卡券列表
     * @apiGroup Admin-Coupon
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} page --
     * @apiParam {Int} page_num --
     *
     * @apiSuccessExample {json} Success-Response:
     *
     * {
     * "code": 1,
     * "msg": "操作成功!",
     * "data": {
     * "count": "1",
     * "list": [
     * {
     * "id": "1",
     * "money": "1.00",
     * "name": "1",
     * "start_time": "1970-01-01 08:00",
     * "end_time": "1970-01-01 08:00",
     * "dateline": "1"
     * }
     * ],
     * "page": 1
     * }
     * }
     */
    public function getList()
    {
     
        $page = \Request::get('page', 1);
        $limit = \Request::get('page_num', 10);
        $param = [];
        $where = '1=1';
        $count = \DB::select("select count(*) as c  from coupon WHERE {$where}", $param)[0]->c;
        $param['offset'] = ($page - 1) * $limit;
        $param['limit'] = $limit;
        $res = \DB::select("select * from coupon WHERE {$where} ORDER BY id DESC limit :offset,:limit", $param);

        $list = [];
        foreach ($res as $v) {

            $v->start_time = date('Y-m-d H:i', $v->start_time);
            $v->end_time = date('Y-m-d H:i', $v->end_time);
            $list[] = $v;
        }
        $data = [
            'count' => $count,
            'list' => $list,
            'page' => $page,
        ];
        return JsonRespose::success($data);
    }

    /**
     * @api {post} /adminapi/coupon/save 添加修改
     * @apiGroup Admin-Coupon
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} id 可选,需要修改的id
     *
     * @apiSuccessExample {json} Success-Response:
     *
     *  {
     * "code": 1,
     * "msg": "操作成功!",
     * "data": 3
     * }
     */
    public function postSave()
    {
        $id = \Request::get('id');
        $money = \Request::get('money');
        $name = \Request::get('name');
        $start_time = \Request::get('start_time');
        $end_time = \Request::get('end_time');

        $start_time = strtotime($start_time);
        $end_time = strtotime($end_time);

        if ($id) {
            $res = CouponTable::ORM()->where('id', $id)->update([
                'money' => $money,
                'name' => $name,
                'start_time' => $start_time,
                'end_time' => $end_time,
                'dateline' => time()
            ]);
        } else {
            $res = CouponTable::ORM()->insertGetId([
                'money' => $money,
                'name' => $name,
                'start_time' => $start_time,
                'end_time' => $end_time,
                'dateline' => time()
            ]);
        }
        return JsonRespose::success($res);
    }

    /**
     * @api {post} /adminapi/coupon/del 删除卡券
     * @apiGroup Admin-Coupon
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} id 可选,需要修改的id
     *
     * @apiSuccessExample {json} Success-Response:
     *
     *  {
     * "code": 1,
     * "msg": "操作成功!",
     * "data": []
     * }
     */
    public function postDel()
    {
        $id = \Request::get('id');
        $res = CouponTable::ORM()->where('id', $id)->delete();

        CouponListTable::ORM()->where('coupon_id', $id)->delete();


        if ($res) {
            return JsonRespose::success();
        } else {
            return JsonRespose::fail();
        }

    }

    /**
     * @api {post} /adminapi/coupon/push 派发
     * @apiGroup Admin-Coupon
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} coupon_id  --
     * @apiParam {Int} phone  手机号
     *
     * @apiSuccessExample {json} Success-Response:
     *
     *  {
     * "code": 1,
     * "msg": "操作成功!",
     * "data": []
     * }
     */
    public function postPush()
    {
        $coupon_id = \Request::get('coupon_id');
        $phone = \Request::get('phone');
        $number = \Request::get('number');


        $user = UserTable::ORM()->where('phone', $phone)->first();

        if (!$user) {
            return JsonRespose::fail('用户不存在');
        }
        $uid = $user->id;

        $coupon = CouponTable::ORM()->where('id', $coupon_id)->first();
        $res = CouponListTable::ORM()->where('uid', $uid)->where('coupon_id', $coupon_id)->first();
        if ($res) {
            $res = CouponListTable::ORM()->where('uid', $uid)->where('coupon_id', $coupon_id)->increment('number',$number);
        } else {
            $res = CouponListTable::ORM()->insert(
                [
                    'money' => $coupon->money,
                    'name' => $coupon->name,
                    'start_time' => $coupon->start_time,
                    'end_time' => $coupon->end_time,
                    'uid' => $uid,
                    'dateline' => time(),
                    'number' => $number,
                    'coupon_id' => $coupon_id
                ]);
        }


        if ($res) {
            return JsonRespose::success();
        } else {
            return JsonRespose::fail();
        }

    }


    /**
     * @api {get} /adminapi/coupon/user-coupon 持券用户列表
     * @apiGroup Admin-Coupon
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} page --
     * @apiParam {Int} page_num --
     *
     * @apiSuccessExample {json} Success-Response:
     *
     * {
     * "code": 1,
     * "msg": "操作成功!",
     * "data": {
     * "count": "1",
     * "list": [
     * {
     * "number": "0",
     * "phone": "13554239049",
     * "nickname": "qinzhiyong",
     * "start_time": "1970-01-01 08:00",
     * "end_time": "1970-01-01 08:00"
     * }
     * ],
     * "page": 1
     * }
     * }
     */
    public function getUserCoupon()
    {
        $page = \Request::get('page', 1);
        $limit = \Request::get('page_num', 100);
        $coupon_id = \Request::get('coupon_id');

        $param = [];
        $where = '1=1';

        if($coupon_id){
            $where .= ' and coupon_id =:coupon_id';
            $param['coupon_id'] =$coupon_id;
        }
        $count = \DB::select("select count(*) as c  from coupon_list WHERE {$where}", $param)[0]->c;
        $param['offset'] = ($page - 1) * $limit;
        $param['limit'] = $limit;
        $res = \DB::select("select coupon_list.id, coupon_list.number,`user`.phone,`user`.nickname from coupon_list LEFT  JOIN  `user` ON  `user`.id=coupon_list.uid WHERE {$where} ORDER BY coupon_list.id DESC limit :offset,:limit", $param);

        $list = [];
        foreach ($res as $v) {

            $v->start_time = date('Y-m-d H:i', $v->start_time);
            $v->end_time = date('Y-m-d H:i', $v->end_time);
            $list[] = $v;
        }
        $data = [
            'count' => $count,
            'list' => $list,
            'page' => $page,
        ];
        return JsonRespose::success($data);
    }


    /**
     * @api {post} /adminapi/coupon/rec 卡券回收
     * @apiGroup Admin-Coupon
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} id 记录id
     *
     * @apiSuccessExample {json} Success-Response:
     *
     * {
    "code": 1,
    "msg": "操作成功!",
    "data": []
    }
     */
    public function postRec()
    {
        $coupon_id = \Request::get('coupon_id');//卡券id
        $id = \Request::get('id');//记录id
        $res = CouponListTable::ORM()->where('coupon_id', $coupon_id)->where('id',$id)->delete();

        return JsonRespose::success();
    }

}